package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Selection;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.BaseApplication;
import com.jucai.pppeiyouclient.bean.AccompanyBean;
import com.jucai.pppeiyouclient.comm.CommonData;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

public class SettingActivity extends BaseActivity {
    private static final String TAG = "SettingActivity : ";
    private TextView settingAccountSafe, settingPerSet, settingLaws, settingAbout, settingLoginOut;
    private Handler mHandler = new Handler();

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);

        ((TextView) findViewById(R.id.common_title_text)).setText("设置");
        settingAccountSafe = (TextView) findViewById(R.id.setting_account_safe);
        settingPerSet = (TextView) findViewById(R.id.setting_per_set);
        settingLaws = (TextView) findViewById(R.id.setting_laws);
        settingAbout = (TextView) findViewById(R.id.setting_about);
        settingLoginOut = (TextView) findViewById(R.id.setting_login_out);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {

    }


    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        settingAccountSafe.setOnClickListener(this);
        settingPerSet.setOnClickListener(this);
        settingLaws.setOnClickListener(this);
        settingAbout.setOnClickListener(this);
        settingLoginOut.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(SettingActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.setting_account_safe:
                startActivity(new Intent(SettingActivity.this, AccountSafeActivity.class));
                break;

            case R.id.setting_per_set:
                startActivity(new Intent(SettingActivity.this, PersonalSettingsActivity.class));
                break;

            case R.id.setting_laws:
                HashMap<String, String> paramLaw = new HashMap<>();
                paramLaw.put("service", "Public_UserIndex.getSinglePageUrl");
                paramLaw.put("article_type","law");
                paramLaw.put("client","accompany");

                CommonRequestNetData.getJsonObjectData(SettingActivity.this, paramLaw, false, new CommonRequestNetData.JsonObjectCallBack() {
                    @Override
                    public void parseJsonObject(JSONObject jsonObject) {
                        try {
                            String url = jsonObject.getString("URL");
                            Intent intent = new Intent(SettingActivity.this, BrowserActivity.class);
                            intent.putExtra("url", url);
                            startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;

            case R.id.setting_about:
                startActivity(new Intent(SettingActivity.this, AboutActivity.class));
                break;

            case R.id.setting_login_out:
                HashMap<String, String> param = new HashMap<>();
                param.put("service", "Client_Login.Logout");
                CommonRequestNetData.getJsonStringData(SettingActivity.this, param, true,new CommonRequestNetData.JsonStringCallBack() {
                    @Override
                    public void parseJsonString(String str) {
                        LogUtils.e(TAG + "str = " + str);
                        ToastUtils.showToast(str);
                        CommonData.clearLoginData(SettingActivity.this);
                        BaseApplication.getBaseApplication().setJPushAliasAndTag();
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                                ActivityCollector.removeActivity(SettingActivity.this);
                            }
                        }, 500);
                    }
                });
                break;

            default:
                break;
        }
    }
}
